package com.kaifamiao.www.Thread.do0927;

import java.time.LocalTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ScheduledThreadPoolDemo {
    public static void main(String[] args) {
        /**
         * ScheduledThreadPool:定长线程池，支持定时及周期性任务执行，线程数量固定，
         * 主要用于执行定时任务和具有固定周期的重复任务
         */
        ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
        System.out.println("当前时间" + LocalTime.now());
        for (int i = 0; i < 3; i++) {
            int number = i;
            service.schedule(new Runnable() {
                @Override
                public void run() {
                    System.out.println("当前时间" + LocalTime.now() + ",线程：" + Thread.currentThread().getName() + ",序号:" + number);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },5,TimeUnit.SECONDS);
        }
    }
}
